package com.atguigu.gmall.gateway.filter;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/8/16 15:41
 * @Version 1.0
 */
@Configuration
public class CorsConfig {

    @Bean
    public WebFilter webFilter(){

        //  创建 CorsConfiguration 对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 设置请求的域名
        corsConfiguration.addAllowedMethod("*"); // 设置请求的方法
        corsConfiguration.addAllowedHeader("*"); // 设置请求头
        corsConfiguration.setAllowCredentials(true);
        //  创建 CorsConfigurationSource 对象
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //  第一个参数指定过滤的范围 第二个参数 : CorsConfiguration 设置规则
        corsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(corsConfigurationSource);
    }

}
